package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.SysRmbSelectdataClass;
import com.limao.DBProvider.SysRmbSelectProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class SysRmbSelect_CacheManager  extends BasicModule{

	private SysRmbSelectProvider provider;
	private static SysRmbSelect_CacheManager instance;

    public static SysRmbSelect_CacheManager getInstance() {
        return instance;
    }
 
    private Cache<String, SysRmbSelectdataClass> SysRmbSelectCache;

    public SysRmbSelect_CacheManager() {
       	super("SysRmbSelect_CacheManager");
       	SysRmbSelectCache = CacheFactory.createCache("cd_SysRmbSelectCache");
    	initProvider();
    	SysRmbSelectCache.clear();
    }
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    //*************************************************************  
    public void DeletingObjectCache()
    {
    	SysRmbSelectCache.clear();
    }
   
    
    public ArrayList<SysRmbSelectdataClass> load_sys_RmbSelect_state1()
    {
      	if(SysRmbSelectCache.size()==0)
      	{
      		ArrayList<SysRmbSelectdataClass> list=provider.load_sys_RmbSelect_state1();
        	for(int i=0;i<list.size();i++)
        	{
        		SysRmbSelectdataClass tmp=list.get(i);
        		SysRmbSelectCache.put(tmp.id, tmp);
        	}
        	return list;
      	}
      	
      	ArrayList<SysRmbSelectdataClass> list=new ArrayList<SysRmbSelectdataClass>();
        Set set =SysRmbSelectCache.keySet();
    	Iterator it=set.iterator();
    	while(it.hasNext()){
     		String s= (String) it.next();
    		list.add(SysRmbSelectCache.get(s));
    	}
    	
    	Collections.sort(list, new Comparator<SysRmbSelectdataClass>() {
    		public int compare(SysRmbSelectdataClass arg0, SysRmbSelectdataClass arg1) {
    				return ((Float)CacheClass.getFloatValue(arg0.rmb)).compareTo(CacheClass.getFloatValue(arg1.rmb));
    		}
    	});

    	return list;
     }
   
    
    
    public SysRmbSelectdataClass Load_SysRmbSelectBy_ID(String id)
    {
      	if(CacheClass.isEmpty(id)) return null;

        SysRmbSelectdataClass obj = SysRmbSelectCache.get(id);
        if (obj == null) {
            synchronized (id.intern()) {
            	obj = SysRmbSelectCache.get(id);
                if (obj == null) {
                	obj=provider.Load_SysRmbSelectBy_ID(id);
                	if(obj!=null)
                	{
                		if(CacheClass.getIntValue(obj.state)==1)
                			SysRmbSelectCache.put(obj.id, obj);
	
                	}
                }
            }
        }
        return obj;
    }

  
    //*************************************************************  
    private void initProvider() {
        if (provider == null ) {
            try {
                provider =SysRmbSelectProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }
  
}
